package com.tao.com.xiaowen.module.main;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.baidu.mobads.AdView;
import com.baidu.mobads.AdViewListener;
import com.baidu.mobads.AppActivity;
import com.tao.com.xiaowen.R;
import com.tao.com.xiaowen.config.ToastUtil;
import com.tao.com.xiaowen.module.main.JokeFragment;
import com.tao.com.xiaowen.module.main.MeFragment;
import com.tao.com.xiaowen.module.main.NewsFragment;
import com.tao.com.xiaowen.module.main.PictureFragment;
import com.tao.com.xiaowen.module.widget.CleanCacheUtil;

import org.json.JSONObject;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private Fragment jokeFragment;
    private Fragment newsFragment;
    private Fragment picFragment;
    private Fragment meFragment;
    public static Fragment currentFragment;

    private RadioGroup radioGroup;

    private AdView adView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        addAd();

        initView();
        addFragment();
    }

    private void addAd() {
        RelativeLayout r = (RelativeLayout) findViewById(R.id.relative);
        AppActivity.setActionBarColorTheme(AppActivity.ActionBarColorTheme.ACTION_BAR_WHITE_THEME);
//        另外，也可设置动作栏中单个元素的颜色, 颜色参数为四段制，0xFF(透明度, 一般填FF)DE(红)DA(绿)DB(蓝)
//        AppActivity.getActionBarColorTheme().set[Background|Title|Progress|Close]Color(0xFFDEDADB);
        // 创建广告View
        String adPlaceId = "4291308"; //  重要：请填上您的广告位ID，代码位错误会导致无法请求到广告
        adView = new AdView(this, adPlaceId);
        // 设置监听器
        adView.setListener(new AdViewListener() {
            public void onAdSwitch() {
                Log.w("", "onAdSwitch");
            }

            public void onAdShow(JSONObject info) {
                // 广告已经渲染出来
                Log.w("", "onAdShow " + info.toString());
            }

            public void onAdReady(AdView adView) {
                // 资源已经缓存完毕，还没有渲染出来
                Log.w("", "onAdReady " + adView);
            }

            public void onAdFailed(String reason) {
                Log.w("", "onAdFailed " + reason);
            }

            public void onAdClick(JSONObject info) {
                Log.w("", "onAdClick " + info.toString());
                cleanCache();
            }

            private void cleanCache() {
                CleanCacheUtil.clearAllCache(MainActivity.this);
            }

            @Override
            public void onAdClose(JSONObject arg0) {
                Log.w("", "onAdClose");
            }
        });
        // 将adView添加到父控件中(注：该父控件不一定为您的根控件，只要该控件能通过addView能添加广告视图即可)
        RelativeLayout.LayoutParams rllp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        rllp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

        r.addView(adView, rllp);
    }


    private void initView() {
        radioGroup = (RadioGroup) findViewById(R.id.radio_group);

        jokeFragment = new JokeFragment();
        newsFragment = new NewsFragment();
        picFragment = new PictureFragment();
        meFragment = new MeFragment();

        radioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
        switch (checkId) {
            case R.id.tab_joke:
                changeFragment(jokeFragment);
                break;
            case R.id.tab_news:
                changeFragment(newsFragment);
                break;
            case R.id.tab_pic:
                changeFragment(picFragment);
                break;
            case R.id.tab_me:
                changeFragment(meFragment);
                break;
            default:
                break;
        }
    }

    private void addFragment() {
        getSupportFragmentManager().beginTransaction().add(R.id.framelayout, jokeFragment).commit();
        getSupportFragmentManager().beginTransaction().add(R.id.framelayout, meFragment).hide(meFragment).commit();
        currentFragment = jokeFragment;
    }

    private void changeFragment(Fragment fragment) {
        if (fragment.isAdded()) {
            getSupportFragmentManager().beginTransaction().hide(currentFragment).show(fragment).commit();
            currentFragment = fragment;
        } else {
            getSupportFragmentManager().beginTransaction().hide(currentFragment).add(R.id.framelayout, fragment).commit();
            currentFragment = fragment;
        }
    }

    @Override
    protected void onDestroy() {
//        adView.destroy();
        super.onDestroy();
    }
}
